/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import store from "../store/index.js";
import myws from "./connector";

export default {
	virtualStickControl(throttle, pitch, roll, yaw, id) {
		myws.sendMessage("/sys/rc", {
			code: "99003",
			param: {
				throttle,
				pitch,
				roll,
				yaw
			}
		}, "摇杆模式", id);
	},
	// settingCameraMode(mode) {
	// 	if (mode === "MSX") {
	// 		myws.sendMessage("/sys/camera", { code: "16019", param: { mode } }, "相机可见光模式");
	// 		setTimeout(() => {
	// 			myws.sendMessage("/sys/camera", { code: "16031", param: { msxLevel: 50 } }, "相机混合模式比例50%");
	// 		}, 500);
	// 	} else {
	// 		myws.sendMessage("/sys/camera", { code: "16019", param: { mode } }, "相机可见光模式");
	// 	}
	// 	// else if (mode === 1) {
	// 	// 	myws.sendMessage("/sys/camera", { code: "16019", param: {mode: mode} }, "相机红外模式");
	// 	// }
	// 	// else if (mode === 2) {
	// 	// 	myws.sendMessage("/sys/camera", { code: "16019", param: {mode: mode} }, "相机混合模式");
	// 	// 	setTimeout(()=>{
	// 	// 		myws.sendMessage("/sys/camera", { code: "16031", param: {msxLevel: 50} }, "相机混合模式比例50%");
	// 	// 	},500)
	// 	// }
	// },
	settingLightMode(mode, id) {
		myws.sendMessage("/sys/camera", {
			code: "16019",
			param: {
				mode
			}
		}, "相机可见光模式", id);
	},
	speakerStartPlay(item) {
		myws.sendMessage("/sys/accessory", {
			code: "18007",
			param: {
				isRepeat: false
			}
		}, "语音不重复");
		setTimeout(() => {
			myws.sendMessage("/sys/accessory", {
				code: "18008",
				param: {
					index: item
				}
			}, "播放");
		}, 200);
	},
	speakerStartPlayRepeatly(item) {
		myws.sendMessage("/sys/accessory", {
			code: "18007",
			param: {
				isRepeat: true
			}
		}, "语音重复");
		setTimeout(() => {
			myws.sendMessage("/sys/accessory", {
				code: "18008",
				param: {
					index: item
				}
			}, "播放");
		}, 200);
	},
	speakerStopPlay() {
		myws.sendMessage("/sys/accessory", {
			code: "18009"
		}, "停止播放");
	},
	getSpeakerPlaylist() {
		myws.sendMessage("/sys/accessory", {
			code: "18011"
		}, "获取语音");
	},

	turnonSpotlight() {
		myws.sendMessage("/sys/accessory", {
			code: "18003",
			param: {
				brightness: 100
			}
		}, "探照灯亮度100");
		setTimeout(() => {
			myws.sendMessage("/sys/accessory", {
				code: "18001"
			}, "打开探照灯");
		}, 200);
	},
	turnoffSpotlight() {
		myws.sendMessage("/sys/accessory", {
			code: "18002"
		}, "关闭探照灯");
	},

	startRecord(id) {
		myws.sendMessage("/sys/camera", {
			code: "16021"
		}, "开始录视频", id);
	},
	stopRecord(id) {
		myws.sendMessage("/sys/camera", {
			code: "16022"
		}, "停止录视频", id);
	},

	resetAirport(id) {
		myws.sendMessage("/sys/power", {
			code: "17013"
		}, "重置机巢", id);
	},
	changeZoom(zoom, id) {
		myws.sendMessage("/sys/camera", {
			code: "16028",
			param: {
				hybridZoomKey: zoom
			}
		}, "变焦", id);
	},
	formatSdCard(id) {
		myws.sendMessage("/sys/media", {
			code: "11007"
		}, "格式化SD卡", id);
	},
	cameraAutoFocus() {
		myws.sendMessage("/sys/camera", {
			code: "16023"
		});
	},

	cancelRTH(id) {
		myws.sendMessage("/sys/rc", {
			code: "99008"
		}, "中断返航", id);
	},

	cancelLanding(id) {
		myws.sendMessage("/sys/rc", {
			code: "99009"
		}, "中断降落", id);
	},

	landImmidately() {
		myws.sendMessage("/sys/rc", {
			code: "99007"
		}, "垂直降落");
	},

	pauseCurrentMission() {
		myws.sendMessage("/sys/mission", {
			code: "10010"
		}, "暂停任务");
	},

	continueCurrentMission() {
		myws.sendMessage("/sys/mission", {
			code: "10011"
		}, "继续任务");
	},

	terminateCurrentMissionAndReturn(id) {
		myws.sendMessage("/sys/mission", {
			code: "10012"
		}, "终止任务");

		setTimeout(() => {
			this.stopRecord(id);
		}, 1000);
		setTimeout(() => {
			this.returnToHome(id);
		}, 3000);
	},

	returnToHome(id) {
		myws.sendMessage("/sys/rc", {
			code: "99005"
		}, "立即返航", id);
	},

	// uploadMission(id, missionContent) {
	// 	myws.sendMessage("/sys/mission", { code: "10003", param: missionContent }, "启动任务", id);
	// },
	selectMissionAndStart(id, missionID) {
		const p = {
			missionID
		};
		myws.sendMessage("/sys/mission", {
			code: "10006",
			param: p
		}, "启动任务", id);
	},

	stopMission(id) {
		myws.sendMessage("/sys/mission", {
			code: "10012"
		}, "终止任务", id);
	},

	powerOnDrone(id) {
		myws.sendMessage("/sys/power", {
			code: "17006"
		}, "飞机电源", id);
	},

	openCabin(id) {
		myws.sendMessage("/sys/motor", {
			code: "15001"
		}, "打开舱们", id);
	},

	closeCabin(id) {
		myws.sendMessage("/sys/motor", {
			code: "15102"
		}, "关闭舱们", id);
	},

	openStream(id) {
		myws.sendMessage("/sys/general", {
			code: "13008"
		}, "打开推流", id);
	},

	stopStream(id) {
		myws.sendMessage("/sys/general", {
			code: "13009"
		}, "关闭推流", id);
	},

	doorOpening(id) {
		myws.sendMessage("/sys/motor", {
			code: "15041"
		}, "紧急开门", id);
	},
	doorCloseing(id) {
		myws.sendMessage("/sys/motor", {
			code: "15042"
		}, "紧急关门", id);
	},
	doorStatusReset(id) {
		myws.sendMessage("/sys/motor", {
			code: "15002"
		}, "紧急关门", id);
	},

	setRTMPStreamAddr(url) {
		const p = {
			url
		};
		myws.sendMessage("/sys/general", {
			code: "13020",
			param: p
		});
	},

	boardRestart() {
		// myws.sendMessage("/sys/power", {'code': '17005'});
	},

	powerOnRemoteControl(id) {
		myws.sendMessage("/sys/rc", {
			code: "99006"
		}, "遥控器电源", id);
	},

	disableVirtualStickMode(id) {
		console.log("测试摇杆")
		myws.sendMessage("/sys/rc", {
			code: "99002"
		}, "关闭摇杆模式", id);
	},

	enableVirtualStickMode(id) {
		console.log("测试摇杆")
		myws.sendMessage("/sys/rc", {
			code: "99001"
		}, "打开摇杆模式", id);
	},

	rotateAircraft(angle) {
		const param = {
			pitch: 0,
			roll: 0,
			yaw: angle,
			throttle: 0
		};
		const stopParam = {
			pitch: 0,
			roll: 0,
			yaw: 0,
			throttle: 0
		};
		myws.sendMessage("/sys/rc", {
			code: "99003",
			param
		});
		// setTimeout(()=>{
		// 	myws.sendMessage("/sys/rc", {'code': '99003', 'param': stopParam });
		// }, 1000);
	},

	setGimbalYAW(angle, id) {
		const param = {
			angle
		};
		myws.sendMessage("/sys/camera", {
			code: "13006",
			param
		}, "云台角度", id);
	},

	// 打开夜灯
	openNightLight(id) {
		myws.sendMessage("/sys/general", {
			code: "13043"
		}, id);
	},

	// 关闭夜灯
	closeNightLight(id) {
		myws.sendMessage("/sys/general", {
			code: "13044"
		}, id);
	},

	closeGather(id) {
		myws.sendMessage("/sys/motor", {
			code: "15011"
		}, "固定飞机", id);
	},
	releaseGather(id) {
		myws.sendMessage("/sys/motor", {
			code: "15012"
		}, "松开飞机", id);
	},

	chargeON(id) {
		myws.sendMessage("/sys/power", {
			code: "17007"
		}, "开始充电", id);
	},
	chargeOFF(id) {
		myws.sendMessage("/sys/power", {
			code: "17008"
		}, "停止充电", id);
	},

	uploadMission(id, content) {
		// const json = JSON.parse(content);
		// @ts-ignore
		const isUpoading = store.state.commandStatus.missionUploading;
		if (isUpoading) return;
		myws.sendMessage(`/sys/mission`, content, "上传任务", id);
		store.commit("commandStatus/updateMissionUploading", true);
		return new Promise((resolve, reject) => {
			let count = 0;
			const interv = setInterval(() => {
				count += 1;
				// @ts-ignore
				if (store.state.commandStatus.missionUploading) {
					console.log("Waiting mission upload finish...");
					if (count > 5) {
						clearInterval(interv);
						store.commit("commandStatus/updateMissionUploading", false);
						reject();
					}
				} else {
					console.log("Mission upload finished.");
					clearInterval(interv);
					resolve();
				}
			}, 1000);
		});
	},

	getSDCardRemaininggSpace() {
		myws.sendMessage("/sys/media", {
			code: "11004"
		}, "获取SD卡剩余容量");
	},

	takePhoto(id) {
		myws.sendMessage("/sys/camera", {
			code: "16020"
		}, "拍照", id);
	},

	// 紧急停止
	emergencyStop(id) {
		myws.sendMessage("/sys/motor", {
			code: "15001"
		}, "打开舱们", id);
		myws.sendMessage("/sys/motor", {
			code: "15012"
		}, "松开飞机", id);
		myws.sendMessage("/sys/mission", {
			code: "10012"
		}, "终止任务", id);
		myws.sendMessage("/sys/rc", {
			code: "99008"
		}, "中断返航", id);
		myws.sendMessage("/sys/rc", {
			code: "99009"
		}, "中断降落", id);
		myws.sendMessage("/sys/rc", {
			code: "99002"
		}, "关闭摇杆模式", id);
	},
	setPhotoMode(mode, shootPhotoMode, id) {
		myws.sendMessage("/sys/camera", {
			code: "16019",
			param: {
				mode: mode
			}
		}, "拍照模式", id);
		myws.sendMessage(
			"/sys/camera", {
				code: "16024",
				param: {
					shootPhotoMode: shootPhotoMode[0]
				}
			},
			"拍照模式",
			id
		);
		if (shootPhotoMode.length > 1) {
			myws.sendMessage(
				"/sys/camera", {
					code: "16024",
					param: {
						shootPhotoMode: shootPhotoMode[1]
					}
				},
				"拍照模式",
				id
			);
		}
	},
	// 格式化SD卡
	formatSDCard(id) {
		myws.sendMessage("/sys/media", {
			code: "20203"
		}, "格式化内存卡", id);
		myws.sendMessage("/sys/media", {
			code: "20204"
		}, "清除遥控器缓存", id);
	},
	// 停止下载数据
	stopDownloadingData(id) {
		myws.sendMessage("/sys/media", {
			code: "20200"
		}, "停止下载数据", id);
	},
	setGoHomeHeight(altitude, id) {
		return new Promise((resolve) => {
			myws.sendMessage("/sys/general", {
				code: 13002,
				param: {
					altitude: altitude
				}
			}, "设置返回高度", id);
			resolve();
		});
	},
	setLiveAddress(url, id) {
		myws.sendMessage("/sys/general", {
			code: 13020,
			param: {
				url: url
			}
		}, "设置直播地址", id);
	}
	// aircraftContentToNest() {
	// 	const baseStatus = store.getters["baseStatus/getStatus"];
	// 	// console.log(baseStatus);
	// 	const { nestStateConstant, isRemoteControllerConnected, isAircraftConnected } = baseStatus;

	// 	new Promise((resovle, reject) => {
	// 		const baseStatus = store.getters["baseStatus/getStatus"];
	// 		const { nestStateConstant } = baseStatus;
	// 		let count = 0;
	// 		const checkInterval = setInterval(() => {
	// 			count++;
	// 			if (count > 20) {
	// 				reject();
	// 			}
	// 			if (nestStateConstant === "STANDBY") {
	// 				clearInterval(checkInterval);
	// 				resovle();
	// 			}
	// 		}, 1000);
	// 	})
	// 		.then(() => {
	// 			if (isRemoteControllerConnected === false) {
	// 				this.powerOnRemoteControl();
	// 			}
	// 			new Promise((resovle, reject) => {
	// 				const baseStatus = store.getters["baseStatus/getStatus"];
	// 				const { isRemoteControllerConnected } = baseStatus;
	// 				let count = 0;
	// 				const checkInterval = setInterval(() => {
	// 					count++;
	// 					if (count > 20) {
	// 						reject();
	// 					}
	// 					if (isRemoteControllerConnected === true) {
	// 						clearInterval(checkInterval);
	// 						resovle();
	// 					}
	// 				}, 1000);
	// 			})
	// 				.then(() => {
	// 					if (isAircraftConnected === false) {
	// 						this.powerOnDrone();
	// 					}
	// 					new Promise((resovle, reject) => {
	// 						let count = 0;
	// 						const checkInterval = setInterval(() => {
	// 							const baseStatus = store.getters["baseStatus/getStatus"];
	// 							const { isAircraftConnected } = baseStatus;
	// 							count++;
	// 							if (count > 20) {
	// 								reject();
	// 							}
	// 							if (isAircraftConnected === true) {
	// 								clearInterval(checkInterval);
	// 								resovle();
	// 							}
	// 						}, 1000);
	// 					})
	// 						.then(() => {
	// 							const executeIDs = store.getters["waypointStatus/getExecuteID"];
	// 							if ((executeIDs as Array<string>).length > 0) {
	// 								const p = { missionID: executeIDs[0] };
	// 								myws.sendMessage("/sys/media", { code: "11008", param: p });
	// 							}
	// 						});
	// 				});
	// 		});
	// },
};